package view;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.List;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import language.Strings;
import language.StringsEN;
import model.AnaliseInfoPerfil;
import model.OperacoesArquivos;
import model.RegistroPerfilMagnetico;
import util.Constantes;
import bean.DadosEntradaRNA;
import bean.PacoteE;

public class StartColeta extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JTextArea jTextArea = null;
	private JLabel lbMa = null;
	private JLabel lbMaConteudo = null;
	private JLabel lbMb = null;
	private JLabel lbMbConteudo = null;
	private JLabel lbTd = null;
	private JLabel lbTdConteudo = null;
	private JLabel lbAa = null;
	private JLabel lbAaConteudo = null;
	private JLabel lbAb = null;
	private JLabel lbAbConteudo = null;
	private JLabel lbNa = null;
	private JLabel lbNaConteudo = null;
	private JLabel lbTa = null;
	private JLabel lbTaConteudo = null;
	private JLabel lbTb = null;
	private JLabel lbTbConteudo = null;
	private JLabel lbSaidaIdeal = null;
	private JLabel lbSaidaIdealConteudo = null;
	private JButton btStart = null;
	private JButton btVerDados = null;
	
	private static boolean executando = false;
	private static boolean gerar_dados = true;
	private JScrollPane jScrollPane = null;

	private StartTestesMetro startTestesMetro = null;
	public static boolean JANELA_ABERTA = false;
	private static int QTD_FLUXO_TEXTAREA = 1;
	
	private StringBuffer bufferFluxoTotal = new StringBuffer();
	private StringBuffer bufferFluxoBruto = null;
	private AnaliseInfoPerfil analiseInfoPerfil = null;
	
	public StartColeta() {
		super();
		initialize();
	}

	private void initialize() {
		this.setSize(387, 400);
		this.setContentPane(getJContentPane());
		this.setTitle(Strings.TITULO_COLETA_DADOS);
		this.setResizable(false);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.setLocation(
						((Toolkit.getDefaultToolkit().getScreenSize().width / 2) - (this
								.getWidth() / 2)),
						((Toolkit.getDefaultToolkit().getScreenSize().height / 2) - (this
								.getHeight() / 2)));
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing (WindowEvent e) {
				Principal.JANELA_ABERTA = false;				
			}			
		});
		
		ocultaItens();
	}

	private void ocultaItens() {
		lbMa.setVisible(false);
		lbMb.setVisible(false);
		lbAa.setVisible(false);
		lbAb.setVisible(false);
		lbTa.setVisible(false);
		lbTb.setVisible(false);
		lbTd.setVisible(false);
		lbNa.setVisible(false);
		lbSaidaIdeal.setVisible(false);
		
		lbMaConteudo.setVisible(false);
		lbMbConteudo.setVisible(false);
		lbAaConteudo.setVisible(false);
		lbAbConteudo.setVisible(false);
		lbTaConteudo.setVisible(false);
		lbTbConteudo.setVisible(false);
		lbTdConteudo.setVisible(false);
		lbNaConteudo.setVisible(false);
		lbSaidaIdealConteudo.setVisible(false);
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			lbSaidaIdealConteudo = new JLabel();
			lbSaidaIdealConteudo.setBounds(new Rectangle(197, 275, 106, 15));
			lbSaidaIdealConteudo.setText("");
			lbSaidaIdeal = new JLabel();
			lbSaidaIdeal.setBounds(new Rectangle(83, 275, 105, 15));
			lbTbConteudo = new JLabel();
			lbTbConteudo.setBounds(new Rectangle(322, 205, 43, 15));
			lbTbConteudo.setText("");
			lbTb = new JLabel();
			lbTb.setBounds(new Rectangle(270, 205, 43, 15));
			lbTaConteudo = new JLabel();
			lbTaConteudo.setBounds(new Rectangle(322, 180, 43, 15));
			lbTaConteudo.setText("");
			lbTa = new JLabel();
			lbTa.setBounds(new Rectangle(270, 180, 43, 15));
			lbNaConteudo = new JLabel();
			lbNaConteudo.setBounds(new Rectangle(260, 230, 43, 15));
			lbNaConteudo.setText("");
			lbNa = new JLabel();
			lbNa.setBounds(new Rectangle(208, 230, 43, 15));
			lbAbConteudo = new JLabel();
			lbAbConteudo.setBounds(new Rectangle(197, 205, 43, 15));
			lbAbConteudo.setText("");
			lbAb = new JLabel();
			lbAb.setBounds(new Rectangle(145, 205, 43, 15));
			lbAaConteudo = new JLabel();
			lbAaConteudo.setBounds(new Rectangle(197, 180, 43, 15));
			lbAaConteudo.setText("");
			lbAa = new JLabel();
			lbAa.setBounds(new Rectangle(145, 180, 43, 15));
			lbTdConteudo = new JLabel();
			lbTdConteudo.setBounds(new Rectangle(132, 230, 43, 15));
			lbTdConteudo.setText("");
			lbTd = new JLabel();
			lbTd.setBounds(new Rectangle(80, 230, 43, 15));
			lbMbConteudo = new JLabel();
			lbMbConteudo.setBounds(new Rectangle(72, 205, 43, 15));
			lbMbConteudo.setText("");
			lbMb = new JLabel();
			lbMb.setBounds(new Rectangle(20, 205, 43, 15));
			lbMaConteudo = new JLabel();
			lbMaConteudo.setBounds(new Rectangle(72, 180, 43, 15));
			lbMaConteudo.setText("");
			lbMa = new JLabel();
			lbMa.setBounds(new Rectangle(20, 180, 43, 15));
			
			lbSaidaIdeal.setText(Strings.TEXT_LABEL_SAIDA_IDEAL + ":");
			lbTb.setText(Strings.TEXT_LABEL_TB + ":");
			lbTa.setText(Strings.TEXT_LABEL_TA + ":");
			lbNa.setText(Strings.TEXT_LABEL_NA + ":");
			lbAb.setText(Strings.TEXT_LABEL_AB + ":");
			lbAa.setText(Strings.TEXT_LABEL_AA + ":");
			lbTd.setText(Strings.TEXT_LABEL_TD + ":");
			lbMb.setText(Strings.TEXT_LABEL_MB + ":");
			lbMa.setText(Strings.TEXT_LABEL_MA + ":");
			
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJTextArea(), null);
			jContentPane.add(lbMa, null);
			jContentPane.add(lbMaConteudo, null);
			jContentPane.add(lbMb, null);
			jContentPane.add(lbMbConteudo, null);
			jContentPane.add(lbTd, null);
			jContentPane.add(lbTdConteudo, null);
			jContentPane.add(lbAa, null);
			jContentPane.add(lbAaConteudo, null);
			jContentPane.add(lbAb, null);
			jContentPane.add(lbAbConteudo, null);
			jContentPane.add(lbNa, null);
			jContentPane.add(lbNaConteudo, null);
			jContentPane.add(lbTa, null);
			jContentPane.add(lbTaConteudo, null);
			jContentPane.add(lbTb, null);
			jContentPane.add(lbTbConteudo, null);
			jContentPane.add(lbSaidaIdeal, null);
			jContentPane.add(lbSaidaIdealConteudo, null);
			jContentPane.add(getBtExecutar(), null);
			jContentPane.add(getBtVerDados(), null);
			jContentPane.add(getJScrollBar(), null);
		}
		return jContentPane;
	}

	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
			jTextArea.setBounds(new Rectangle(17, 15, 347, 147));
			jTextArea.setEditable(false);
		}
		return jTextArea;
	}

	private JButton getBtVerDados() {
		if (btVerDados == null) {
			btVerDados = new JButton();
			btVerDados.setBounds(new Rectangle(205, 320, 110, 25));
			btVerDados.setText(Strings.TEXT_BT_VER_DADOS);
			btVerDados.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(JANELA_ABERTA){
						JOptionPane.showMessageDialog(null, 
								Strings.TEXT_LABEL_MAIS_DE_UMA_JANELA_ABERTA);
						return;
					}
					
					startTestesMetro = null;
					
					if(startTestesMetro == null){
						startTestesMetro = new StartTestesMetro();
					}
										
					startTestesMetro.setVisible(true);
					startTestesMetro.setState(JFrame.NORMAL);
					JANELA_ABERTA = true;
				}
			});
		}
		return btVerDados;
	}
	
	private JButton getBtExecutar() {
		if (btStart == null) {
			btStart = new JButton();
			btStart.setBounds(new Rectangle(55, 320, 110, 25));
			btStart.setText(Strings.TEXT_BT_EXECUTAR);
			btStart.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(executando == false){
						executando = true;
												
						carregaDadosArquivo();
						getBtExecutar().setText(Strings.TEXT_BT_EXECUTAR);
					} else{
						executando = false;
						getBtExecutar().setText(Strings.TEXT_BT_PARAR);
					}
				}
			});
		}
		return btStart;
	}
	
	private void evento(String pacoteStr){
		if(pacoteStr.length() < 1){
			return;
		}
		
		if(QTD_FLUXO_TEXTAREA < 20){
			bufferFluxoTotal = new StringBuffer();
		}
		
		bufferFluxoTotal.append(bufferFluxoTotal);
		getJTextArea().setText(bufferFluxoTotal.toString());
		
		QTD_FLUXO_TEXTAREA++;
		
		char primeiraLetra = pacoteStr.charAt(0);
				
		switch (primeiraLetra) {
			case 'T':
				bufferFluxoBruto.append(pacoteStr + "\n");
				analiseInfoPerfil.eventoDeEntrada(pacoteStr);
								
				break;
			case 'F':
				bufferFluxoBruto.append(pacoteStr + "\n");
				analiseInfoPerfil.eventoDeFluxo(pacoteStr);				
				
				break;
			case 'E':
				bufferFluxoBruto.append(pacoteStr + "\n");
				analiseInfoPerfil.eventoDeSaida(pacoteStr);
							
				File arquivoOriginal = new File(Constantes.DIRETORIO_PERFIL_TESTES_METROLOGICO_FLUXO_ORIGINAL, pacoteStr + Constantes.EXTENSAO_TXT);
				String nomeDoArquivo = arquivoOriginal.getAbsolutePath();
				
				OperacoesArquivos.salvaDadosArquivoTexto(bufferFluxoBruto.toString(), nomeDoArquivo, false);
				bufferFluxoBruto = new StringBuffer();
				
				break;
			default:
				
				break;
		}
	}
	
	public void carregaDadosArquivo(){
		JFileChooser chooser = new JFileChooser();
		if(Constantes.IDIOMA_ATUAL.equals(Constantes.IDIOMA_EN)){
			chooser.setLocale(Locale.ENGLISH);
		} else{
			chooser.setLocale(getLocale());
		}
		chooser.updateUI();
		chooser.setCurrentDirectory(Constantes.DIRETORIO_RAIZ);
		chooser.setMultiSelectionEnabled(false);
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		chooser.setFileFilter(UtilitariosGUI.extencao(2));
		
		int resp = chooser.showOpenDialog(StartColeta.this);
		
		if(resp == JFileChooser.APPROVE_OPTION){
			File arquivoSelecionado = chooser.getSelectedFile();
			List<String> fluxosBruto = OperacoesArquivos.lerTxt(arquivoSelecionado.getAbsolutePath());
			
			bufferFluxoBruto = new StringBuffer();
			analiseInfoPerfil = new AnaliseInfoPerfil();
			
			for (String pacoteString : fluxosBruto) {
				evento(pacoteString);
			}
			
			JOptionPane.showMessageDialog(null, Strings.TEXT_LABEL_DADOS_OBTIDOS_MSG);
		} else{
			executando = false;
			getBtExecutar().setText(Strings.TEXT_BT_PARAR);
		}
	}
	
	private JScrollPane getJScrollBar() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane(getJTextArea());
			jScrollPane.setBounds(new Rectangle(17, 15, 347, 147));
		}
		jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		jScrollPane.setPreferredSize(new Dimension(3, 18));
		jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		
		return jScrollPane;
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
